using JT808Protocol.Common;
using JT808Protocol.Models.BodyParserStrategy.Others;
using JT808Protocol.Models.Enums;
using System;
using System.Collections.Generic;

namespace JT808Protocol.Models.BodyParserStrategy
{
    /// <summary>
    /// 外设系统信息
    /// 长度：0～32
    /// 名称：采用 ASCII 表示(例如：软件版本号SV1.1.0
    /// 表示为0x53 0x56 0x31 0x2E 0x31 0x2E 0x30)
    /// 客户代码为用户代码，由外设厂家自定义
    /// </summary>

    internal class ItemPeripheralInfo : IJT808Body
    {
        public ItemPassthroughPeripheralBase PassthroughPeripheralBase { get; set; }

        /// <summary>
        /// 公司名称长度
        /// </summary>
        public byte CompanyNameLength { get; set; }

        /// <summary>
        /// 公司名称
        /// </summary>
        public string CompanyName { get; set; }

        /// <summary>
        /// 产品型号长度
        /// </summary>
        public byte ProductTypeLength { get; set; }

        /// <summary>
        /// 产品型号
        /// </summary>
        public string ProductType { get; set; }

        /// <summary>
        /// 硬件版本号长度
        /// </summary>
        public byte DeviceVerLength { get; set; }

        /// <summary>
        /// 硬件版本号
        /// </summary>
        public string DeviceVer { get; set; }

        /// <summary>
        /// 软件版本号长度
        /// </summary>
        public byte SoftVerLength { get; set; }

        /// <summary>
        /// 软件版本号
        /// </summary>
        public string SoftVer { get; set; }

        /// <summary>
        /// 设备ID 长度
        /// </summary>
        public byte DeviceIDLength { get; set; }

        /// <summary>
        /// 设备ID
        /// </summary>
        public string DeviceID { get; set; }

        /// <summary>
        /// 客户代码长度
        /// </summary>
        public byte CustomCodeLength { get; set; }

        /// <summary>
        /// 客户代码
        /// </summary>
        public string CustomCode { get; set; }

        public override IJT808Body ReadBody(ReadOnlySpan<byte> source, ref int index)
        {
            PassthroughPeripheralBase = new ItemPassthroughPeripheralBase();

            var idByte = source[index];
            if (Enum.IsDefined(typeof(EnumPeripheralType), idByte))
            {
                PassthroughPeripheralBase.AdditionId = (EnumPeripheralType)idByte;
            }
            else
            {
                PassthroughPeripheralBase.AdditionId = EnumPeripheralType.Undefine;
            }
            index += 1;

            PassthroughPeripheralBase.DataLength = (byte)(source[index] + 2);
            index += 1;

            CompanyNameLength = source[index];
            index += 1;

            CompanyName = ParseHelper.AsciiString(ref source, index, CompanyNameLength);
            index += CompanyNameLength;

            ProductTypeLength = source[index];
            index += 1;

            ProductType = ParseHelper.AsciiString(ref source, index, ProductTypeLength);
            index += ProductTypeLength;

            DeviceVerLength = source[index];
            index += 1;

            DeviceVer = ParseHelper.AsciiString(ref source, index, DeviceVerLength);
            index += DeviceVerLength;

            SoftVerLength = source[index];
            index += 1;

            SoftVer = ParseHelper.AsciiString(ref source, index, SoftVerLength);
            index += SoftVerLength;

            DeviceIDLength = source[index];
            index += 1;

            DeviceID = ParseHelper.AsciiString(ref source, index, DeviceIDLength);
            index += DeviceIDLength;

            CustomCodeLength = source[index];
            index += 1;

            CustomCode = ParseHelper.AsciiString(ref source, index, CustomCodeLength);

            return this;
        }

        public override List<byte> WriteToBytes()
        {
            throw new NotImplementedException();
        }
    }
}